package com.telit.firecommand.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.telit.android.dto.CommMsgCenterLogDTO;
import com.telit.common.entity.DataReturn;
import com.telit.common.entity.PageData;
import com.telit.firecommand.dto.CommMsgCenterLog;
import com.telit.firecommand.dto.QueryMsgCenterDTO;
import com.telit.firecommand.service.impl.MsgCenterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Auther amy
 * @Describe 消息中心-推送数据
 */
@RestController
@RequestMapping("msg-center")
@Api(tags = "消息中心-推送数据", description = "/msg-center")
public class MsgCenterController {

    @Autowired
    private MsgCenterService msgCenterService;

    @GetMapping(value = "/listByPage", produces = "application/json;charset=UTF-8")
    @ApiOperation("消息中心-分页列表")
    public DataReturn listByPage(@RequestParam(defaultValue = "1") Integer current,
                                 @RequestParam(defaultValue = "10") Integer size,
                                 QueryMsgCenterDTO queryMsgCenterDTO) {
        IPage<CommMsgCenterLog> commMsgCenterLogIPage = msgCenterService.listByPage(current, size, queryMsgCenterDTO);
        return DataReturn.ok(new PageData<>((int) commMsgCenterLogIPage.getTotal(), current, size,
                commMsgCenterLogIPage.getRecords()));
    }

    @GetMapping(value = "/detail/{id}", produces = "application/json;charset=UTF-8")
    @ApiOperation("消息中心-消息详情")
    public DataReturn detail(@PathVariable("id") Long id) {
        return DataReturn.ok(msgCenterService.detail(id));
    }

    @PostMapping(value = "/add", produces = "application/json;charset=UTF-8")
    @ApiOperation("消息中心-消息新增")
    public DataReturn add(@Validated @RequestBody CommMsgCenterLog commMsgCenterLog) {
        return DataReturn.ok(msgCenterService.add(commMsgCenterLog));
    }

    @PutMapping(value = "/updateStatus/{status}", produces = "application/json;charset=UTF-8")
    @ApiOperation("消息中心-修改消息状态")
    public DataReturn update(@RequestParam("ids") List<Long> ids, @PathVariable("status") String status) {
        return DataReturn.ok(msgCenterService.updateStatus(ids, status));
    }

    @PostMapping(value = "/addBatch", produces = "application/json;charset=UTF-8")
    @ApiOperation("消息中心-批量消息新增")
    public DataReturn addBatch(@Validated @RequestBody CommMsgCenterLogDTO dto) {
        msgCenterService.addBatch(dto);
        return DataReturn.ok();
    }

    @GetMapping(value = "/unReadCount", produces = "application/json;charset=UTF-8")
    @ApiOperation("消息中心-未读消息条数")
    public DataReturn unReadCount(@RequestParam("userId") Long userId,
                                  @RequestParam(value = "msgType", required = false) String msgType) {
        QueryMsgCenterDTO queryMsgCenterDTO = new QueryMsgCenterDTO();
        queryMsgCenterDTO.setMsgType(msgType);
        queryMsgCenterDTO.setHasRead("0");// 未读状态
        queryMsgCenterDTO.setUserId(userId);
        return DataReturn.ok(msgCenterService.unReadCount(queryMsgCenterDTO));
    }

}
